<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="排课ID" prop="sortCurriculumId">-->
<!--        <el-input-->
<!--          v-model="queryParams.sortCurriculumId"-->
<!--          placeholder="请输入排课ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="课节名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入课节名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['jiaopei:kj:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['jiaopei:kj:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="openSelectStudent"
          v-hasPermi="['jiaopei:kjStudent:add']"
        >新增临时学员</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handlekjStudent"
          v-hasPermi="['jiaopei:kjStudent:add']"
        >刷新学员</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jiaopei:kj:remove']"
        >删除</el-button>
      </el-col>

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['jiaopei:kj:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="mytable" v-loading="loading" :data="kjList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="true" min-width="60"  show-overflow-tooltip />
      <el-table-column label="排课ID" align="center" prop="sortCurriculumId"min-width="60"  show-overflow-tooltip  />
      <el-table-column label="班级名称" align="center" prop="classVo.name" min-width="80"  show-overflow-tooltip />
      <el-table-column label="课节名称" align="center" prop="name" min-width="100"  show-overflow-tooltip />
      <el-table-column label="课程时间" align="center" prop="courseDuration" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.courseDuration, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startHour" min-width="80" />
      <el-table-column label="结束时间" align="center" prop="endHour" min-width="80" />

      <el-table-column label="学员数量" align="center" prop="sl" min-width="80" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="showXy(scope.row)"
            v-hasPermi="['jiaopei:kjStudent:list']"
          >{{scope.row.sl}}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createdDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right"  align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['jiaopei:kj:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['jiaopei:kj:remove']"-->
<!--          >删除</el-button>-->

          <el-button
            size="mini"
            type="text"
            @click="showXy(scope.row)"
            v-hasPermi="['jiaopei:kjStudent:list']"
          >学员</el-button>


        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课节对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级ID" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入班级ID" />
        </el-form-item>
        <el-form-item label="排课ID" prop="sortCurriculumId">
          <el-input v-model="form.sortCurriculumId" placeholder="请输入排课ID" />
        </el-form-item>
        <el-form-item label="课节名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课节名称" />
        </el-form-item>
        <el-form-item label="课程时间" prop="courseDuration">
          <el-date-picker clearable
                          v-model="form.courseDuration"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择课程时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="开始时间" prop="startHour">
          <el-time-select
            style="width:180px"
            size="small"
            placeholder="起始时间"
            v-model="form.startHour"
            :picker-options="{start:form.startHour||'06:00',step: '00:10',end: '23:00'}">
          </el-time-select>
          <el-time-select
            style="width:180px;margin-left:16px;"
            size="small"
            placeholder="结束时间"
            v-model="form.endHour"
            :picker-options="{start:form.endHour||'06:30',step: '00:10',end: '23:00',minTime:form.startHour}">
          </el-time-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <select-student ref="selectStudent"  @ok="openSelectStudentOk" />


  </div>
</template>

<script>
import {listKj, getKj, delKj, addKj, updateKj, refreshKjXy} from "@/api/jiaopei/kj";
import {addKjStudent} from "@/api/jiaopei/kjStudent";
import selectStudent from "@/views/jiaopei/student/selectStudent";


export default {
  name: "Kj",
  dicts: ['kj_student_status'],
  components: { selectStudent },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课节表格数据
      kjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: undefined,
        className: undefined,
        sortCurriculumId: undefined,
        name: undefined,
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "班级ID不能为空", trigger: "blur" }
        ],
        sortCurriculumId: [
          { required: true, message: "排课ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "课节名称不能为空", trigger: "blur" }
        ],
        courseDuration: [
          { required: true, message: "课程时间不能为空", trigger: "blur" }
        ],
        createdDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        startHour: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endHour: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
      },
      leftUrl:undefined
    };
  },
  watch: {
    $route: {
      immediate: true,
      handler(to, from) {
        if(to.path=='/yewu/kj'){
          if(this.leftUrl!=to.fullPath){
            if(this.$route.query['sortCurriculumId']){
              this.queryParams.sortCurriculumId=this.$route.query['sortCurriculumId'];
            }
            this.getList();
          }
          this.leftUrl=to.fullPath;
        }
      }
    }
  },
  created() {
    if(this.$route.query['sortCurriculumId']){
      this.queryParams.sortCurriculumId=this.$route.query['sortCurriculumId'];
    }
    this.getList();
  },
  methods: {
    /** 查询课节列表 */
    getList() {


      this.loading = true;
      listKj(this.queryParams).then(response => {
        this.kjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        classId: undefined,
        sortCurriculumId: undefined,
        name: undefined,
        startHour:undefined,
        endHour:undefined,
        courseDuration: undefined,
        createdDate: undefined
      };
      this.resetForm("form");
    },
    // 表单重置
    reset1() {
      this.form1 = {
        id: undefined,
        classId: undefined,
        kjId: undefined,
        studentId: undefined,
        studentName: undefined,
        curriculumId: undefined,
        lessonHours: undefined,
        status: '1',
        photo: undefined,
        signDate: undefined,
        createdDate: undefined
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课节";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getKj(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改课节";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateKj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addKj(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课节编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delKj(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jiaopei/kj/export', {
        ...this.queryParams
      }, `kj_${new Date().getTime()}.xlsx`)
    },
    showXy(row){
      const kjId = row.id;
      this.$router.push({path:'/yewu/kjStudent', query:{'kjId':kjId}})
    },
    handlekjStudent(row){
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认刷新课节学员信息？').then(() => {
        this.loading = true;
        return refreshKjXy(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("刷新成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    openSelectStudent(){
      const selectedRows = this.$refs.mytable.selection;
      let row=selectedRows[0];

      this.reset1();
      this.form1.curriculumId=row['classVo']['curriculumId'];
      this.form1.classId=row['classId'];
      this.form1.kjId=row['id'];


      this.$refs.selectStudent.show(true,{'curriculumId':this.form1['curriculumId'] });
    },
    openSelectStudentOk(row){
      this.form1.studentId=row['id'];
      this.form1.studentName=row['name'];
      addKjStudent(this.form1).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });

    }

  }
};
</script>
